package cn.ppwx.crm.modules.sys.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import cn.ppwx.crm.common.log.CrmLogger;
import cn.ppwx.crm.common.utils.StringUtils;

public class HttpTookit {
	private static final CloseableHttpClient httpClient;
	public static final String CHARSET = "UTF-8";

	static {
		RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(15000).build();
		httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
	}

	public static String doGet(String url, Map<String, String> params) {
		return doGet(url, params, CHARSET);
	}

	public static String doPost(String url, Map<String, String> params, boolean hasParam) {
		return doPost(url, params, CHARSET, hasParam);
	}

	/*
	 * public static String doPost(String url, String params) { return
	 * doPost(url, params, CHARSET); }
	 */

	private static String doGet(String url, Map<String, String> params, String charset) {
		if (StringUtils.isBlank(url)) {
			return null;
		}
		HttpGet httpGet = null;
		CloseableHttpResponse response = null;
		try {
			if (params != null && !params.isEmpty()) {
				List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
				for (Map.Entry<String, String> entry : params.entrySet()) {
					String value = entry.getValue();
					if (value != null) {
						pairs.add(new BasicNameValuePair(entry.getKey(), value));
					}
				}
				url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
			}
			httpGet = new HttpGet(url);
			response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				throw new RuntimeException("HttpClient,error status code :" + statusCode);
			}
			HttpEntity entity = response.getEntity();
			String result = null;
			if (entity != null) {
				result = EntityUtils.toString(entity, "utf-8");
			}
			EntityUtils.consume(entity);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (httpGet != null) {
				httpGet.releaseConnection();
			}

			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {

				}
			}
		}

		return null;
	}

	private static String doPost(String url, Map<String, String> params, String charset, boolean hasParam) {
		if (StringUtils.isBlank(url)) {
			return null;
		}
		try {

			HttpPost httpPost = new HttpPost(url);

			if (hasParam) {
				List<NameValuePair> pairs = null;
				if (params != null && !params.isEmpty()) {
					pairs = new ArrayList<NameValuePair>(params.size());
					for (Map.Entry<String, String> entry : params.entrySet()) {
						String value = entry.getValue();
						if (value != null) {
							pairs.add(new BasicNameValuePair(entry.getKey(), value));
						}
					}
				}

				if (pairs != null && pairs.size() > 0) {
					httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
				}
			} else {
				if (params != null && params.containsKey("param") && params.get("param").length() > 0) {
					
					StringEntity entity = new StringEntity(params.get("param").toString(), CHARSET);// 解决中文乱码问题
					entity.setContentEncoding("UTF-8");
					entity.setContentType("application/json");
					httpPost.setEntity(entity);
				}
			}

			CloseableHttpResponse response = httpClient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpPost.abort();
				throw new RuntimeException("HttpClient,error status code :" + statusCode);
			}
			HttpEntity entity = response.getEntity();
			String result = null;
			if (entity != null) {
				result = EntityUtils.toString(entity, "utf-8");
			}
			EntityUtils.consume(entity);
			return result;
		} catch (Exception e) {
			CrmLogger.log.error("post error:" + e);
		}
		return null;
	}

	/*
	 * private static String doPost(String url, String params, String charset) {
	 * if (StringUtils.isBlank(url)) { return null; }
	 * 
	 * try {
	 * 
	 * HttpPost httpPost = new HttpPost(url);
	 * 
	 * if (params != null && params.length() > 0) { StringEntity entity1 = new
	 * StringEntity(params.toString(), CHARSET);// 解决中文乱码问题
	 * entity1.setContentEncoding("UTF-8");
	 * entity1.setContentType("application/json"); httpPost.setEntity(entity1);
	 * }
	 * 
	 * 
	 * CloseableHttpResponse response = httpClient.execute(httpPost); int
	 * statusCode = response.getStatusLine().getStatusCode(); if (statusCode !=
	 * 200) { httpPost.abort(); throw new
	 * RuntimeException("HttpClient,error status code :" + statusCode); }
	 * HttpEntity entity = response.getEntity(); String result = null; if
	 * (entity != null) { result = EntityUtils.toString(entity, "utf-8"); }
	 * EntityUtils.consume(entity); return result; } catch (Exception e) {
	 * CrmLogger.log.error("post error:" + e); } return null; }
	 */
}
